package com.zttek.videoShareProject.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zttek.videoShareProject.common.json.CommentVO;
import com.zttek.videoShareProject.common.json.Result;
import com.zttek.videoShareProject.common.keyword.CommentUttil;
import com.zttek.videoShareProject.common.utils.DateUtils;
import com.zttek.videoShareProject.modules.mapper.CommentMapper;
import com.zttek.videoShareProject.modules.mapper.KeywordMapper;
import com.zttek.videoShareProject.modules.mapper.UserMapper;
import com.zttek.videoShareProject.modules.model.Comment;
import com.zttek.videoShareProject.modules.model.Keyword;
import com.zttek.videoShareProject.modules.model.User;
import com.zttek.videoShareProject.modules.service.ICommentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

import static java.lang.Math.E;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CBY
 * @since 2018-07-03
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private KeywordMapper keywordMapper;

    public Comment selectCommentById(Long comment_id) throws Exception {
        Comment comment = commentMapper.selectById(comment_id);
        return comment;
    }

    public CommentVO.CommentForAddReturn addComment(Comment comment) throws Exception {

        EntityWrapper<Keyword> ew = new EntityWrapper<Keyword>();
        ew.setEntity(new Keyword());
        List<Keyword> list = keywordMapper.selectList(ew);

        /*检查context*/
        String context = CommentUttil.checkContext(comment.getContext(),list);
        comment.setContext(context);
        commentMapper.insert(comment);
        User u = userMapper.selectById(comment.getUserId());
        CommentVO.CommentForAddReturn commentForAddReturn = new CommentVO.CommentForAddReturn();
        commentForAddReturn.setAccount(u.getAccount());
        commentForAddReturn.setComment_id(comment.getCommentId());
        commentForAddReturn.setContext(comment.getContext());
        commentForAddReturn.setImage_url(u.getImageUrl());
        commentForAddReturn.setComment_date(DateUtils.formatDate(comment.getCommentDate()));
        return commentForAddReturn;
    }

    public List<CommentVO.CommentForAddReturn> selectCommentByVideoId (Long video_id)throws Exception{
        EntityWrapper<Comment> ew=new EntityWrapper<Comment>();
        ew.setEntity(new Comment());
        ew.eq("video_id",video_id);
        List<Comment> commentList =commentMapper.selectList(ew);
        List<CommentVO.CommentForAddReturn> list = new ArrayList<CommentVO.CommentForAddReturn>();
        User user = null;
        for(int i=0;i<commentList.size();i++)
        {
            CommentVO.CommentForAddReturn commentForAddReturn = new CommentVO.CommentForAddReturn();
            user =userMapper.selectById(commentList.get(i).getUserId());
//            System.out.println(user);
            commentForAddReturn.setAccount(user.getAccount());
            commentForAddReturn.setComment_id(commentList.get(i).getCommentId());
            commentForAddReturn.setContext(commentList.get(i).getContext());
            commentForAddReturn.setImage_url(user.getImageUrl());
            commentForAddReturn.setComment_date(DateUtils.formatDate(commentList.get(i).getCommentDate()));
            list.add(commentForAddReturn);
        }
        return list;
    }



}
